/*
 * $Id: ErrorHandlerMDP.java,v 1.2 2009/08/07 15:39:38 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.errorHandler;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.database.dao.ICodeDetailsDAO;
import com.ge.healthcare.autosc.common.database.dao.ITaskDAO;
import com.ge.healthcare.autosc.common.database.pojo.ActRequest;
import com.ge.healthcare.autosc.common.database.pojo.ActTask;
import com.ge.healthcare.autosc.common.message.converter.AutoSCMessage;
import com.ge.healthcare.autosc.common.message.sender.MessageSender;

/**
 * @author 212042764
 * 
 */
public class ErrorHandlerMDP {
	private static ICodeDetailsDAO codeDAO = null;
	private static ITaskDAO taskDAO = null;
	private static MessageSender messageSender = null;

	private static final String EHM_TMR_001 = "EHM_TMR_001";

	/**
	 * Spring likes me
	 */
	public ErrorHandlerMDP() {
		super();
	}

	/**
	 * 
	 * @param ascMsg
	 */
	public void process(AutoSCMessage ascMsg) {
		String methodName = "process";
		ASCLogger.debug(this.getClass(), methodName,
				"Received object message : " + ascMsg);
		try {
			if (ascMsg instanceof ActTask) {
				this.processTask((ActTask) ascMsg);
			} else if (ascMsg instanceof ActRequest) {
				this.processRequest((ActRequest) ascMsg);
			} else {
				ASCLogger.error(this.getClass(), methodName,
						"Received unknown msg object : " + ascMsg);
			}
		} catch (RuntimeException runtimeExcep) {
			ASCLogger.error(this.getClass(), methodName,
					"Failed to process with Exception:" + runtimeExcep,
					runtimeExcep);
			throw runtimeExcep;
		}
	}

	/**
	 * Sets the failure code on task object
	 * 
	 * @param task
	 */
	private void processTask(ActTask task) {
		Long codeId = codeDAO.getUniqueCodedetailsByCodeName(EHM_TMR_001)
				.getCodeId();
		task.setCodeId(codeId);
		taskDAO.save(task);
		messageSender.sendToQueue(task);
	}

	/**
	 * Sets failure code on request object
	 * 
	 * @param request
	 */
	private void processRequest(ActRequest request) {
		String methodName = "processRequest";
		/*
		 * We are just raising a fault tracker because the only component
		 * currently sending the request object to dispatcher is scheduler and
		 * if scheduler can't forward the request to dispatcher then error mdp
		 * on scheduler won't be able to forward request to dispatcher either
		 */
		ASCLogger.error(this.getClass(), methodName,
				"Received failed Request Object:" + request, new Exception(
						"Failed request:" + request));
	}

	/**
	 * @param codeDAO
	 *            the codeDAO to set
	 */
	public void setCodeDAO(ICodeDetailsDAO codeDAO) {
		ErrorHandlerMDP.codeDAO = codeDAO;
	}

	/**
	 * @param messageSender
	 *            the messageSender to set
	 */
	public void setMessageSender(MessageSender messageSender) {
		ErrorHandlerMDP.messageSender = messageSender;
	}

	/**
	 * @param taskDAO
	 *            the taskDAO to set
	 */
	public void setTaskDAO(ITaskDAO taskDAO) {
		ErrorHandlerMDP.taskDAO = taskDAO;
	}
}
